package com.css.fxfzypg.modules.baseData.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.repository.RiskEnterpriseDataRepository;
import com.css.fxfzypg.modules.baseData.entity.RiskEnterpriseData;
import com.css.fxfzypg.modules.baseData.service.RiskEnterpriseDataService;
import com.css.fxfzypg.modules.baseData.queryParams.RiskEnterpriseDataQueryParams;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class RiskEnterpriseDataServiceImpl implements RiskEnterpriseDataService {
    @Autowired
    RiskEnterpriseDataRepository riskEnterpriseDataRepository;

//    @Override
//@Transactional(rollbackFor = Exception.class)
//    public RiskEnterpriseDataVO updateRiskEnterpriseData(RiskEnterpriseDataVO riskEnterpriseDataVO) {
//
//        RiskEnterpriseData riskEnterpriseData = riskEnterpriseDataRepository.updateElevation(riskEnterpriseDataVO);
//
//        return riskEnterpriseData;
//    }

    @Override
    @Transactional
    public  RiskEnterpriseData saveRiskEnterpriseData( RiskEnterpriseData riskEnterpriseData) {

        riskEnterpriseData.setRedCreatetime(new Date());
        riskEnterpriseData.setRedStatus(YNEnum.Y.toString());

        return riskEnterpriseDataRepository.saveRiskEnterpriseData(riskEnterpriseData);
    }

    @Override
    public JSONObject getAllRiskEnterpriseData(RiskEnterpriseDataQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(riskEnterpriseDataRepository.getAllRiskEnterpriseDataParams(queryParams));
    }

    @Override
    public RiskEnterpriseData getRiskEnterpriseDataById(Integer id) {
        RiskEnterpriseData riskEnterpriseData = riskEnterpriseDataRepository.getRiskEnterpriseDataById(id);
        return riskEnterpriseData;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRiskEnterpriseDatas(List<Integer> ids) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        riskEnterpriseDataRepository.deleteRiskEnterpriseDatasById(ids,timestamp);
    }

    @Override
    public List<RiskEnterpriseData> getAll() {

        return riskEnterpriseDataRepository.getAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<RiskEnterpriseData> riskEnterpriseData = FileUtil.importExcel(file, RiskEnterpriseData.class);

        for(int i = 0 ; i<riskEnterpriseData.size();i++){
            riskEnterpriseData.get(i).setRedCreatetime(PlatformDateUtils.getCurrentTimestamp());
            riskEnterpriseData.get(i).setRedStatus(YNEnum.Y.toString());

        }
        riskEnterpriseDataRepository.importExcel(riskEnterpriseData);
    }

    @Override
    public List<RiskEnterpriseData> getriskData(String taskId) {
        List<RiskEnterpriseData> list=riskEnterpriseDataRepository.getriskData(taskId);
        return list;
    }
}
